package org.example.controller;

import org.example.model.common.dtos.ResponseResult;
import org.example.model.common.dtos.user.UserBasicDto;
import org.example.model.common.vos.user.LoginVo;
import org.example.model.common.vos.user.SearchUserVo;
import org.example.model.user.UserBasic;
import org.example.service.UserBasicService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 */
@CrossOrigin
@RestController
@RequestMapping("/user-basic")
public class UserBasicController {

    @Autowired
    private UserBasicService userBasicService;

    /**
     * 用户登录
     */
    @PostMapping("/login/{flag}")
    public ResponseResult Login(@RequestBody LoginVo loginVo,@PathVariable Boolean flag) {
        return ResponseResult.okResult(userBasicService.Login(loginVo,flag));
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public ResponseResult Register(@RequestBody LoginVo loginVo) {
        return ResponseResult.okResult(userBasicService.Register(loginVo));
    }
    /**
     * 新增用户
     */
    @PostMapping("/user-basic")
    public ResponseResult AddUser(@RequestBody UserBasic userBasic) {
        return ResponseResult.okResult(userBasicService.InsertUserBasic(userBasic));
    }

    /**
     * 根据Id查询数据详情
     */
    @GetMapping("/user-basic/{id}")
    public ResponseResult GetUserById(@PathVariable int id) {
        return ResponseResult.okResult(userBasicService.SelectUserById(id));
    }

    /**
     * 根据id修改用户信息
     */
    @PutMapping("/user-basic")
    public ResponseResult UpdateUserById(@RequestBody UserBasic userBasic) {
        return ResponseResult.okResult(userBasicService.UpdateUserBasicById(userBasic));
    }

    /**
     * 修改密码
     */
    @PutMapping("/password")
    public ResponseResult UpdatePassword(@RequestBody UserBasic userBasic) {
        return ResponseResult.okResult(userBasicService.UpdateUserBasicById(userBasic));
    }

    /**
     * 按条件(用户名模糊查询，登录账号模糊查询，用户状态精确查询,用户类型精确查询)分页查询
     * @return
     */
    @GetMapping("/user-list/{pageIndex}/{pageSize}")
    public ResponseResult SelectPageByCondition(SearchUserVo searchUserVo, @PathVariable int pageIndex, @PathVariable int pageSize) {
        UserBasic userBasic = new UserBasic();
        BeanUtils.copyProperties(searchUserVo,userBasic);
        return ResponseResult.okResult(userBasicService.SelectPageByConditions(userBasic,pageIndex,pageSize));
    }

    /**
     * 根据id删除用户
     */
    @DeleteMapping("/user-basic/{id}")
    public ResponseResult DeleteUserBasicById(@PathVariable int id) {
        return ResponseResult.okResult(userBasicService.DeleteUserBasicById(id));
    }
}
